package me.softwareengineer.guice.rendering.domain.referenece;

import org.apache.commons.lang3.StringUtils;

/**
 * Represents document reference
 * 
 * @author yadtahir
 * 
 */
public class DocumentResourceReference extends ResourceReference {

	/**
	 * The name of the parameter representing the Query String.
	 */
	private static final String QUERY_STRING = "queryString";

	/**
	 * The name of the parameter representing the Anchor.
	 */
	private static final String ANCHOR = "anchor";

	/**
	 * Creates document reference
	 * 
	 * @param reference
	 *            the reference of the document
	 */
	public DocumentResourceReference(String reference) {
		super(reference, ResourceType.DOCUMENT);
	}

	/**
	 * @return the anchor name pointing to an anchor defined in the referenced
	 *         document or null if no anchor has been specified (in which case
	 *         the reference points to the top of the document). Note that in
	 *         XWiki anchors are automatically created for titles. Example:
	 *         "TableOfContentAnchor"
	 */
	public String getAnchor() {
		return (String) getMetaData(ANCHOR);
	}

	/**
	 * @return the query string for specifying parameters that will be used in
	 *         the rendered URL or null if no query string has been specified.
	 *         Example: "mydata1=5&mydata2=Hello"
	 */
	public String getQueryString() {
		return (String) getMetaData(QUERY_STRING);
	}

	/**
	 * @param anchor
	 *            see {@link #getAnchor()}
	 */
	public void setAnchor(String anchor) {
		if (!StringUtils.isEmpty(anchor)) {
			addMetaData(ANCHOR, anchor);
		}
	}

	/**
	 * @param queryString
	 *            see {@link #getQueryString()}
	 */
	public void setQueryString(String queryString) {
		if (!StringUtils.isEmpty(queryString)) {
			addMetaData(QUERY_STRING, queryString);
		}
	}
}
